#!/bin/sh

if [ "$(hostname)" == "$(hostname -s)" ] ; then
	echo "-------------------------------------------------------------------"
	echo " WARNING:"
	echo " - Need change hostname with qualify to FQDN"
	echo "   (use 'server1.domain.com' instead 'server1')"
	echo " - May trouble for web and mail without FQDN hostname"
	echo "-------------------------------------------------------------------"
	echo " - For OpenVZ VPS, change hostname from VPS panel"
	echo " - For Others, change 'HOSTNAME' in '/etc/sysconfig/network' file"
	echo " - Reboot after change hostname"
	echo
	exit
fi

if rpm -qa|grep 'yum-plugin-replace' >/dev/null 2>&1 ; then
	echo ""
else
	yum -y install yum-plugin-replace
fi

checktmpfs=$(cat /etc/fstab|grep '/tmp'|grep 'tmpfs')

if [ "${checktmpfs}" != "" ] ; then
	echo "This server have '/tmp' with 'tmpfs' detect."
	echo "Modified '/etc/fstab' where remove 'tmpfs' in '/tmp' line and then reboot."
	echo "Without remove, backup/restore may have a trouble."
	exit
fi

## ST -- importance for Centos 6
unalias cp > /dev/null 2>&1; unalias mv > /dev/null 2>&1; unalias rm > /dev/null 2>&1

if [ ! -f /script/programname ] ; then
	echo 'kloxo' > /script/programname
fi

if rpm -qa|grep lxphp >/dev/null 2>&1 ; then
	rpm -e lxphp --nodeps
	rpm -e lxlighttpd --nodeps
	rpm -e lxzend --nodeps
	
	'rm' -rf /usr/local/lxlabs/ext >/dev/null 2>&1
fi

if rpm -qa|grep php52s >/dev/null 2>&1 ; then
	yum remove php52s -y
	sh /script/phpm-installer php53s
fi

if rpm -qa|grep php53s >/dev/null 2>&1 ; then
	yum remove php53s* -y
	rpm -e php53s-fpm --noscript

	sh /script/phpm-installer php53s
fi

if rpm -qa|grep mysql56u >/dev/null 2>&1 ; then
	echo "Already use mysql56u. No replace"
elif rpm -qa|grep mysql55 >/dev/null 2>&1 ; then
	echo "Already use mysql55. No replace"
elif rpm -qa|grep MariaDB-server >/dev/null 2>&1 ; then
	echo "Already use MariaDB. No replace"
else
	if rpm -qa|grep mysql >/dev/null 2>&1 ; then
		echo "Replace mysql to mysql56u"
		yum replace mysql --replace-with=mysql56u -y
	fi
fi

ppath="/usr/local/lxlabs/kloxo"

if ! [ -d ${ppath}/log ] ; then
	### must create log path because without it possible segfault for php!
	mkdir -p ${ppath}/log
fi

if [ "$1" == "--remove-kloxo-databse" ] || [ "$2" == "--remove-kloxo-databse" ] || [ "$3" == "--remove-kloxo-databse" ] \
		|| [ "$1" == "-r" ] || [ "$2" == "-r" ] || [ "$3" == "-r" ] ; then
	if [ -f /etc/rc.d/init.d/mysql ] ; then
		service mysql stop
		'rm' -rf /var/lib/mysql/kloxo
		service mysql start
	else
		service mysqld stop
		'rm' -rf /var/lib/mysql/kloxo
		service mysqld start
	fi
fi

if [ "$1" == "-y" ] || [ "$1" == "--force" ] || [ "$2" == "-y" ] || \
		[ "$2" == "--force" ]  || [ "$3" == "-y" ] || [ "$3" == "--force" ] ; then
	if [ -f /etc/rc.d/init.d/qmail ] ; then
		service qmail stop
		'rm' -f /etc/rc.d/init.d/qmail
	fi

	sh ${ppath}/install/setup.sh $*  | tee ${ppath}/install/install.log

	## ST -- running 'upcp -y ' make skin always use 'simplicity' skin
	sh /script/skin-set-all-client >/dev/null 2>&1
else
	if [ -d /var/lib/mysql/kloxo ] ; then
		sh /script/cleanup
	else
		sh ${ppath}/install/setup.sh $*  | tee ${ppath}/install/install.log
	fi
fi
#yum install -y --skip-broken kloxost-*
#yum install -y kloxost7-thirdparty-phpmyadmin
